package com.mingqijia.gassafety.webserver.strategy.impl;

import com.mingqijia.gassafety.shared.constant.MessagePushTypeEnum;
import com.mingqijia.gassafety.shared.constant.warnStrategy.NotifyObjTypeTypeEnum;
import com.mingqijia.gassafety.webserver.dto.SendMessageDTO;
import com.mingqijia.gassafety.webserver.service.strategy.WarnStrategyV2ServiceImpl;
import com.mingqijia.gassafety.webserver.strategy.MessageStrategy;
import com.mingqijia.gassafety.webserver.strategy.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 推送微信消息 策略实现
 * </p>
 *
 * @author mby
 * @since 2024-01-16
 */
@Service
@Slf4j
public class SendLocalMessageStrategy implements MessageStrategy { // 实现策略

    @Autowired
    WarnStrategyV2ServiceImpl warnStrategyV2Service;

    /**
     * 发送消息
     * @param data 发送数据
     * @param job job名称
     * @param recordService 通知记录保存服务
     */
    @Override
    public void sendMessage(List<SendMessageDTO> data, String job, RecordService recordService) {
        if (CollectionUtils.isNotEmpty(data)) {
            data.stream()
                .filter(x -> x.getPushTypeCode().equals(MessagePushTypeEnum.LOCAL.getValue()))
                .forEach(x -> {
                    try {
                        x.setNotifyObjType(NotifyObjTypeTypeEnum.LOCAL.getCode());
                        log.info("{}-sendMessage-local:imei:{},phones:{},phones:{}", job, x.getImei(), x.getPhone(), x.getPhones() == null ? "" : x.getPhones());
                        warnStrategyV2Service.pushLocal(x, job, recordService);
                    } catch (Exception e) {
                        log.info("{}-sendMessage-local-error:{}", job, e);
                    }
                });
        }

    }
}
